﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using LH.Model.ST;
using XXX.Bo;
using System.ServiceModel;
using ServiceReference1;
using XXX.Common;
using System.Xml.Linq;
using LH.Model.PU;
using LH.Model.SA;
using fuzhu;
using LH.Model.APAR;
using FastMember;

namespace U8COWeb
{
    /// <summary>
    /// 发货单单据接口
    /// </summary>
    public static class DispatchlistCO
    {
        /// <summary>
        /// 构建发货单的xml
        /// </summary>
        /// <param name="rdrecord"></param>
        /// <returns></returns>
        //public static void GetRd01Xml(SA_DispatchList rdrecord, out string xdomhead, out string xdombody)
        //{
        //    //3、构建表头及表体的xml
        //    //        System.Xml.Linq.XDocument xhead = new XDocument();
        //    //        //添加根节点,表头表体都
        //    //        XNamespace n1 = @"urn:schemas - microsoft - com:rowset";
        //    //        XNamespace n2 = @"#RowsetSchema";
        //    //        XElement rootEle = new XElement(n1 + "data", new XAttribute(XNamespace.Xmlns + "rs", n1.NamespaceName)
        //    //);
        //    //        XElement classEle = new XElement(n2 + "row", new XAttribute(XNamespace.Xmlns + "z", n2.NamespaceName)
        //    //);

        //    //        Type type = rdrecord.head.GetType();
        //    //        //添加属性
        //    //        XAttribute attrClass; 
        //    //        foreach (PropertyInfo p in type.GetProperties())
        //    //        {

        //    //            if (p.GetValue(rdrecord.head) != null)
        //    //            {
        //    //                attrClass = new XAttribute(p.Name, p.GetValue(rdrecord.head));
        //    //                classEle.Add(attrClass);
        //    //            }

        //    //        }
        //    //        ////审核标记，写审核人
        //    //        //attrClass = new XAttribute("iStatus", "1");
        //    //        //classEle.Add(attrClass);
        //    //        rootEle.Add(classEle);
        //    //        xhead.Add(rootEle);

        //    //        System.Xml.Linq.XDocument xbody = new XDocument();
        //    //        //添加根节点,表头表体都
        //    //        List<SA_DispatchList.Body> lst = rdrecord.bodies;
        //    //        XElement rootElebody = new XElement(n1 + "data", new XAttribute(XNamespace.Xmlns + "rs", n1.NamespaceName)
        //    //        );
        //    //        //多行，要循环写
        //    //        for (int i = 0; i < lst.Count; i++)
        //    //        {
        //    //            SA_DispatchList.Body rdrecords = lst[i];
        //    //            XElement classElebody = new XElement(n2 + "row", new XAttribute(XNamespace.Xmlns + "z", n2.NamespaceName)
        //    //        );
        //    //            Type typeBody = rdrecords.GetType();
        //    //            //添加属性
        //    //            //attrClass = new XAttribute("editprop", "A");
        //    //            //classElebody.Add(attrClass);

        //    //            //遍历属性值，有值得写过去
        //    //            foreach (PropertyInfo p in typeBody.GetProperties())
        //    //            {

        //    //                if (p.GetValue(rdrecords) != null)
        //    //                {

        //    //                    attrClass = new XAttribute(p.Name, p.GetValue(rdrecords));
        //    //                    classElebody.Add(attrClass);

        //    //                }

        //    //            }

        //    //            rootElebody.Add(classElebody);
        //    //        }
        //    //        xbody.Add(rootElebody);
        //    //        xdomhead = xhead.ToString();
        //    //        xdombody = xbody.ToString();
        //    XNamespace rsNs = "urn:schemas-microsoft-com:rowset";
        //    XNamespace zNs = "#RowsetSchema";

        //    var rootHead = new XElement(rsNs + "data", new XAttribute(XNamespace.Xmlns + "rs", rsNs));
        //    var rowHead = new XElement(zNs + "row", new XAttribute(XNamespace.Xmlns + "z", zNs));

        //    var accessor = TypeAccessor.Create(rdrecord.head.GetType());

        //    foreach (var member in accessor.GetMembers())
        //    {
        //        var value = accessor[rdrecord.head, member.Name];
        //        if (value != null)
        //        {
        //            rowHead.Add(new XAttribute(member.Name, value));
        //        }
        //    }

        //    rootHead.Add(rowHead);
        //    xdomhead = rootHead.ToString();

        //    var rootBody = new XElement(rsNs + "data", new XAttribute(XNamespace.Xmlns + "rs", rsNs));
        //    foreach (var bodyItem in rdrecord.bodies)
        //    {
        //        var rowBody = new XElement(zNs + "row", new XAttribute(XNamespace.Xmlns + "z", zNs));

        //        var accessorbody = TypeAccessor.Create(bodyItem.GetType());

        //        foreach (var member in accessorbody.GetMembers())
        //        {
        //            var value = accessorbody[bodyItem, member.Name];
        //            if (value != null)
        //            {
        //                rowBody.Add(new XAttribute(member.Name, value));
        //            }
        //        }


        //        rootBody.Add(rowBody);
        //    }
        //    xdombody = rootBody.ToString();
        //}

        #region 新增发货单 
        /// <summary>
        /// 新增采购订单,返回ok或者error
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="err">错误信息</param>
        /// <param name="id">新增单据的id</param>
        /// <param name="ccode">新增单据的code</param>
        /// <returns></returns>
        public static bool AddVerifySA_DispatchList(SA_DispatchList entity, string xmlconfig, out string err, out string id, out string ccode)
        {
            try
            {
                for (int i = 0; i < entity.bodies.Count; i++)
                {
                    entity.bodies[i].editprop = "A";
                }

                string xdomhead = XmlOperation.GetCOXmlHead<SA_DispatchList.Head>(entity.head);
                string xdombody = XmlOperation.GetCOXmlBody<SA_DispatchList.Body>(entity.bodies);

                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                binding.OpenTimeout = new TimeSpan(0, 2, 0); // 设置打开超时时间为 5 分钟
                binding.SendTimeout = new TimeSpan(0, 2, 0); // 设置发送超时时间为 5 分钟
                binding.ReceiveTimeout = new TimeSpan(0, 2, 0); // 设置接收超时时间为 5 分钟
                binding.CloseTimeout = new TimeSpan(0, 2, 0); // 设置关闭超时时间为 5 分钟
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);
               
                var result = callClient.execAsync("DP,SA", "9", "add,verify", "", xdomhead, xdombody, "", xmlconfig, "config").Result;
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("cSubID=" + "DP,SA");
                sb.AppendLine("cVouchType=" + "12发货单新增审核");
                sb.AppendLine("cOptType=" + "add,verify");
                sb.AppendLine("cVouchID=" + "");
                sb.AppendLine("domHead=" + xdomhead);
                sb.AppendLine("domBody=" + xdombody);
                sb.AppendLine("sErrorMsg=" + "");
                sb.AppendLine("domSub=" + xmlconfig);
                sb.AppendLine("other=" + "config");
                sb.AppendLine("CO=" + result.ToString());
                ExLog.WriteTxt("CO", sb.ToString()); 
                /*var xmlResult = XDocument.Parse(result); //实例化XmlDocument,怎么用这个,网上去查查*/
                return XmlOperation.GetCoResult_addverify("新增发货单", "1", out err, out id, out ccode, result);
            }
            catch (Exception ex)
            {
                id = "";
                ccode = "";
                err = ex.Message;
                return false;
            }
        }

        #endregion




      


        #region 变更发货单 
        /// <summary>
        /// 变更采购订单,返回ok或者error
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="err">错误信息</param>
        /// <param name="id">新增单据的id</param>
        /// <param name="ccode">新增单据的code</param>
        /// <returns></returns>
        public static bool ChangeSA_DispatchList(SA_DispatchList entity,string xmlConfig, out string err, out string id, out string ccode)
        {
            try
            {

                string xdomhead = XmlOperation.GetCOXmlHead<SA_DispatchList.Head>(entity.head);
                string xdombody = XmlOperation.GetCOXmlBody<SA_DispatchList.Body>(entity.bodies);

                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                binding.OpenTimeout = new TimeSpan(0, 2, 0); // 设置打开超时时间为 5 分钟
                binding.SendTimeout = new TimeSpan(0, 2, 0); // 设置发送超时时间为 5 分钟
                binding.ReceiveTimeout = new TimeSpan(0, 2, 0); // 设置接收超时时间为 5 分钟
                binding.CloseTimeout = new TimeSpan(0, 2, 0); // 设置关闭超时时间为 5 分钟
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("cSubID=" + "DP,SA");
                sb.AppendLine("cVouchType=" + "12");
                sb.AppendLine("cOptType=" + "change");
                sb.AppendLine("cVouchID=" + "");
                sb.AppendLine("domHead=" + xdomhead);
                sb.AppendLine("domBody=" + xdombody);
                sb.AppendLine("sErrorMsg=" + "");
                sb.AppendLine("domSub=" + xmlConfig);
                sb.AppendLine("other=" + "config");
                ExLog.WriteTxt("CO", sb.ToString());
                var result = callClient.execAsync("DP,SA", "9", "change,verify", entity.head.dlid, xdomhead, xdombody, "", xmlConfig, "config").Result;
                ExLog.WriteTxt("CO", result.ToString());
                return XmlOperation.GetCoResult("新增发货单", "0", out err, out id, out ccode, result);
            }
            catch (Exception ex)
            {
                id = "";
                ccode = "";
                err = ex.Message;
                return false;
            }
        }

        #endregion

        #region 审核/弃审/删除发货单
        /// <summary>
        /// 审核/弃审/删除发货单
        /// </summary>
        /// <param name="ID">销售单ID</param>
        /// <param name="coperation">操作：delete，verify，unverify</param>
        /// <returns></returns>
        public static bool OperateSA_DispatchList(String ID, string xmlconfig,string coperation, out string err)
        {
            try
            {
              

                var binding = new BasicHttpBinding();
                binding.MaxReceivedMessageSize = 2147483647;
                binding.OpenTimeout = new TimeSpan(0, 1, 0); // 设置打开超时时间为 5 分钟
                binding.SendTimeout = new TimeSpan(0, 1, 0); // 设置发送超时时间为 5 分钟
                binding.ReceiveTimeout = new TimeSpan(0, 1, 0); // 设置接收超时时间为 5 分钟
                binding.CloseTimeout = new TimeSpan(0, 1, 0); // 设置关闭超时时间为 5 分钟
                // 根据 WebService 的 URL
                var endpoint = new EndpointAddress(CanShu.coUrl);
                // 创建调用接口的工厂，注意这里泛型只能传入接口
                var factory = new ChannelFactory<WebService1Soap>(binding, endpoint);
                // 从工厂获取具体的调用实例
                var callClient = factory.CreateChannel();
                // 调用具体的方法，这个地方就看你都有啥方法啦，自行替换
                //var request = new SendFormatSMSRequest(sendFormatSMSRequestBody);
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("cSubID=" + "DP,SA");
                sb.AppendLine("cVouchType=" + "9");
                sb.AppendLine("cOptType=" + coperation);
                sb.AppendLine("cVouchID=" + ID.ToString());
                sb.AppendLine("domHead=" + "");
                sb.AppendLine("domBody=" + "");
                sb.AppendLine("sErrorMsg=" + "");
                sb.AppendLine("domSub=" + xmlconfig);
                sb.AppendLine("other=" + "config");
                ExLog.WriteTxt("CO", sb.ToString());
                var result = callClient.execAsync("DP,SA", "9", coperation, ID.ToString(), "", "", "", xmlconfig, "config").Result;
                ExLog.WriteTxt("CO", result.ToString());

                return XmlOperation.GetCoResult("发货单"+coperation, "0", out err, out _, out _, result);
            }
            catch (Exception ex)
            {

                err = ex.Message;
                return false;
            }
        }
        #endregion

    }
}
